
template <typename T>
class UnionFindSet
{
public:
    UnionFindSet()
    {
    }

    template <typename TContainer>
    UnionFindSet(const TContainer &container)
    {
        auto begin = container.begin();
        auto end = container.end();
        while (begin != end)
        {
            m[*begin] = *begin;
            ++begin;
            ++_groupCount;
        }
    }

    void insert(const T &value)
    {
        if (!m.count(value))
        {
            m[value] = value;
            ++_groupCount;
        }
    }

    bool exist(const T &value)
    {
        return m.count(value);
    }

    T find(const T &value)
    {
        if (m[value] == value)
        {
            return value;
        }
        return m[value] = find(m[value]);
    }

    void join(const T &value1, const T &value2)
    {
        if (!m.count(value1))
        {
            m[value1] = value1;
            ++_groupCount;
        }
        if (!m.count(value2))
        {
            m[value2] = value2;
            ++_groupCount;
        }
        if (find(value1) != find(value2))
        {
            m[find(value1)] = find(value2);
            --_groupCount;
        }
    }

    int groupCount()
    {
        return _groupCount;
    }

private:
    std::unordered_map<T, T> m;
    int _groupCount = 0;
};
class Solution
{
public:
    int countCompleteComponents(int n, vector<vector<int>> &edges)
    {
        UnionFindSet<int> s;
        for (int i = 0; i < n; ++i)
        {
            s.insert(i);
        }

        vector<int> nodeDegree(n, 0);
        for (auto &edge : edges)
        {
            s.join(edge[0], edge[1]);
            ++nodeDegree[edge[0]];
            ++nodeDegree[edge[1]];
        }

        unordered_map<int, vector<int>> groups;
        for (int i = 0; i < n; ++i)
        {
            groups[s.find(i)].push_back(i);
        }

        int count = 0;
        for (auto &[_, group] : groups)
        {
            bool valid = true;
            for (int node : group)
            {
                if (nodeDegree[node] != group.size() - 1)
                {
                    valid = false;
                    break;
                }
            }
            if (valid)
            {
                ++count;
            }
        }
        return count;
    }
};